<template>
    <div class="box_b">
        <div class="register">
            <div class="register-title">登 陆</div>
            <div class="content-form">
                <el-form ref="formRef" :model="form" label-width="80px" :rules="rules">
                    <el-form-item label="账号" prop="username">
                        <el-input
                            v-model="form.username"
                            placeholder="请输入账号"
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input
                            v-model="form.password"
                            type="password"
                            show-password
                            placeholder="请输入密码"
                        ></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <button class="register_i" @click="clickToLogin" v-loading="loading">
                登录
            </button>
        </div>
    </div>
</template>

<script>
import {login} from "@/admin/api/admin/login/login";

export default {
    data() {
        return {
            form: {
                username: "admin",
                password: "123456",
            },
            loading: false,
            // 验证码规则
            identifyCodes: "3456789ABCDEFGHJKMNPQRSTUVWXYabcdefghjkmnpqrstuvwxy",
            identifyCode: "",
            rules: {
                username: [{required: true, message: "请输入账号", trigger: "blur"}],
                password: [
                    {required: true, message: "请输入密码", trigger: "change"},
                ],
                // identifyCodeValue: [
                //   { required: true, message: "请输入验证码", trigger: "change" },
                // ],
            },
            formRef: null,
        };
    },
    methods: {
        clickToLogin() {
            this.submitForm();
        },
        handleKeyPress(event) {
            if (event.keyCode === 13) { // 按下回车键
                this.clickToLogin(); // 调用登录方法
            }
        },
// 登陆
        submitForm() {
            this.loading = true;
            login({
                username: this.form.username,
                password: this.form.password,
            }).then((res) => {
                this.loading = false;
                if (res.code === 200) {
                    localStorage.setItem("adminToken", res.data.token);
                    this.$message.success(res.message);
                    this.$router.push("/admin");
                } else {
                    this.$message.error(res.message);
                }
            });
        }
        ,
    },

    mounted() {
        // this.makeCode();
        document.addEventListener('keydown', this.handleKeyPress);
    },
    beforeDestroy() {
        // 在组件销毁前移除键盘事件监听，避免内存泄漏
        document.removeEventListener('keydown', this.handleKeyPress);
    },
}
;
</script>


<style lang="scss" scoped>
.box_b {
    height: 90%;
    background: linear-gradient(0deg, rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2));
    background-image: url("/src/img/bgi.jpg");
    background-size: cover;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: flex-start;
    padding-top: 10vh;
}

.box_b .title {
    z-index: 10000;
    color: #ffffffbb;
    text-align: center;
    font-size: 70px;
    letter-spacing: 0.5vw;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0);
    margin-bottom: 140px;
}

@keyframes fadeInAndColorChange {
    0% {
        opacity: 0;
        color: rgba(0, 0, 0, 0.5);
        text-shadow: 0 0 5px rgba(255, 255, 255, 0);
    }

    100% {
        opacity: 1;
        color: #fff;
        text-shadow: 0 0 10px gray;
    }
}

@keyframes scaleUp {
    0% {
        transform: scale(0.95);
    }

    100% {
        transform: scale(1.55);
    }
}

.box_b::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background-color: rgba(255, 255, 255, 0.5);
    /* 白色遮罩层，透明度0.5 */
}

.box_b img {
    width: 500px;
    height: 550px;
}

.register {
    width: 550px;
    height: 400px;
    background-color: #ffffffec;
    border-radius: 15px;
    padding: 40px;

    .register-title {
        font-size: 45px;
        text-align: center;
        margin-bottom: 60px;
        font-weight: bold;
    }
}

.register h1 {
    text-align: center;
    line-height: 80px;
}

.register span {
    font-size: 20px;
}

.register_r {
    width: 100%;
    display: flex;
    justify-content: space-evenly;
    line-height: 60px;
    text-align: center;
    align-items: center;
}

.register_r .el-input {
    width: 300px;
    height: 35px;
    outline: none;
    padding-left: 10px;
    border: none;
}

.register_e {
    margin-top: 1vh;
    width: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    line-height: 60px;
    text-align: center;
    align-items: center;
}

.register_e .el-input {
    width: 200px;
    height: 35px;
    outline: none;
    margin-left: -4px;
    line-height: 35px;
    border: none;
}

.register_i {
    background-color: #298dfb;
    width: 84%;
    line-height: 50px;
    color: #fff;
    margin-top: 5%;
    border-radius: 5px;
    text-align: center;
    margin-left: 8%;
    border: 1px #e4e7ed ridge;
    font-size: 20px;
    cursor: pointer;
}

#c1 {
    vertical-align: middle;
    box-sizing: border-box;
    cursor: pointer;
    margin-right: 10px;
}
</style>
